package com.server.tools;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class OPPOMiniPay {
	public static String callback_url = "https://server.dreamgear82.com/pay/callback.php";
	public static String orequest_url = "https://jits.open.oppomobile.com/jitsopen/api/pay/v1.0/preOrder";
	
	private static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALHwvP8a3ghgt3+oh+IGKcvW2kFXSU+ddhCcO/JDBVm2D3GhNAcOuS4zGhNGZwhSI2209ELnRPRpT6+XUvN4sORWwJvNFgXR7MwjdWfUlqeX3q/I+S6rYTzDTOAQRqa9U/oDXCt2QRCq2pLFkqpTSnmzTO4hMvf/7dSj6f97GTMNAgMBAAECgYEAr7d6C7mudTt9OdVlinBnqeHb2hamULbANpryH8TLbdH54k48oJOLjvHr2Aj4CiGC1v2RENhtIac1h8K5qbEPjGSdqfDaFm6Eu/mLzCkaQPY0koIwwrSEQ89ws5wfRm9pzeEVMgtZXPe5WQ34I6D5OmNvf25Is4seNdR8urOz0oECQQDyfwCMxCYUhhcwSQ9QAVm5ud98aZKzdf0A2bS9geV1ZlDm5K6XaRQygtJ+Wb8Wz5x32aptGbdw/6zsh162TmF9AkEAu9lv1xecSUJui+6vyoxzg/sujvyXTbx+B7f+L7ffj/+1fSFISknmI+ehPLgqOqYoNFit45zxhpO1xJZAiNzM0QJACjwN++LzcH5QUDtlKA1Gii4sHoYeKPDb4sNNe4oV8ySEWZphPAa1UYRv+eEzzXSpt+054izprx07YSg9eRhJyQJAFc0Yc8gjtUTme4sKD+HZDjo6nxGlpbn4joDGGVentJiiwZD5SNXmxcf+KIPfMstR4zFfjQkL5uiBsf0WJEwr4QJAN44tQqNiqJNdh/Y9TDumdBYwC/uSfyatctRB9zMTDSrAE5ezQS8M16vvwW/XgbwYvDEp2AhdaPv1Oqf05Z2Lgw==";
	/**
	 * 发送订单请求
	 * @param appid 应用id
	 * @param openId 玩家open id
	 * @param productName 商品名
	 * @param productDesc 商品介绍
	 * @param count 商品数量
	 * @param price 商品价格
	 * @param cpOrderId 运营方订单号
	 * @param appVersion 应用版本号
	 * @param engineVersion 引擎版本号
	 * @return OPPO返回的数据
	 */
	public static String SendOrder(String appid, String openId, String productName, String productDesc, int count, int price,String cpOrderId, String appVersion,String engineVersion,Long timestamp) {
		TreeMap<String, String> map = new TreeMap<String, String>();
		//按照字符ASCII码顺序添加参数
		map.put("appId", appid);
		map.put("appVersion", appVersion);
		map.put("callBackUrl", callback_url);
		map.put("count", String.valueOf(count));
		map.put("cpOrderId", cpOrderId);
		map.put("currency", "CNY");
		map.put("engineVersion", engineVersion);
		map.put("openId", openId);
		map.put("price", String.valueOf(price));
		map.put("productDesc", productDesc);
		map.put("productName", productName);
		map.put("timestamp", String.valueOf(timestamp));
		
		//参数签名
		Set<String> keys = map.keySet();
	    StringBuilder sb = new StringBuilder();
	    Iterator<String> iterator = keys.iterator();
	    while (iterator.hasNext()) {
	    	String key = iterator.next();
	    	sb.append(key).append("=").append(map.get(key)).append("&");
	    }
	    String signContent = sb.toString().substring(0, sb.length() - 1);
	    
	    //加入签名
	    String sign = RSASignature.sign(signContent, privateKey);
		map.put("sign", sign);
		
		//向OPPO统一下单接口发送订单请求
	    String response = HttpUtils.GetHttpData(orequest_url, JSON.toJSONString(map), "POST");
		
	    return response;
	}
	
	/**
	 * 获取支付签名
	 * @param appKey 应用秘钥
	 * @param orderNo OPPO订单号
	 * @param timestamp 时间戳
	 * @return 支付签名
	 */
	public static String GetPaySignature(String appKey, String orderNo, Long timestamp) {
		return RSASignature.sign("appKey=" + appKey + "&orderNo=" + orderNo + "&timestamp=" + timestamp, privateKey);
	}
	
	
	
	public static void main(String[] args) throws Exception{
		Long timestamp = System.currentTimeMillis();
	    String data = SendOrder("30639981", "TN_dmhkN3BNWWQ3T0RCNHdIUnlzc2JVc0ljRnZxUit5TWNhODJhWlM3MXpyeUN4ekdaZmhoTXVlU3FyZ0l0c05ieA"
	    		,"测试商品", "这是一个简单的测试商品！", 1, 1,"test" + timestamp,"1.0.0","1073",timestamp);
		System.out.println(data);
	}
}
